param (
	$OutputPath = 'doc'
)

function New-HelpSkeleton {

	begin {
		$_Cmdlet = [Management.Automation.Cmdlet]
		$_CmdletAttribute = [Management.Automation.CmdletAttribute]
		$_ParameterAttribute = [Management.Automation.ParameterAttribute]
		
		if(-not (test-path $OutputPath)) {
			new-item -type Directory $OutputPath
		}

		$OutputPath = (resolve-path $OutputPath)
		
		function EnsureDirectory($namespace) {
			$dir = $OutputPath
			$parts = $namespace.Split('.')
			
			1..($parts.Length - 1) |% {
				$dir = join-path $dir $parts[$_]

				if(-not (test-path $dir)) {
					new-item -type Directory $dir | out-null
				}
			}

			"$dir"
		}
	}

	process {
		$_.GetExportedTypes() |? { $_Cmdlet.IsAssignableFrom($_) } |% {
		
			$attr = $_.GetCustomAttributes($_CmdletAttribute, $false)
			if($attr) {
			
				$dir = EnsureDirectory $_.Namespace
				$filename = (join-path $dir "$($_.Name).xml")
				
				$description = ''
				$detailed = ''
				
				$_.GetCustomAttributes($false) |% {

					$type = $_.GetType().FullName
					
					if($type -eq 'System.ComponentModel.DescriptionAttribute') {
						$description = $_.Description
					}
					
					if($type -eq 'Pscx.DetailedDescriptionAttribute') {
						$detailed = $_.Text
					}					
				}
				
				"<?xml version='1.0' ?>"                       > $filename
				"<Cmdlet FullName='$($_.Fullname)'>"          >> $filename
				"    <Description>"                           >> $filename
				"         $description"                       >> $filename
				"    </Description>"                          >> $filename

				"    <DetailedDescription>"                   >> $filename
				"         $detailed"                          >> $filename
				"    </DetailedDescription>"                  >> $filename

				"    <Parameters>"                            >> $filename
				
				$_.GetProperties('Public,Instance,FlattenHierarchy') |% {
					
					$pa = @($_.GetCustomAttributes($_ParameterAttribute, $false))[0]
					
					if($pa) {
						$description = ($pa.HelpMessage)
						$default = ''

						$_.GetCustomAttributes($false) |% {
							$type = $_.GetType().FullName
							
							if($type -eq 'System.ComponentModel.DefaultValueAttribute') {
								$default = $_.Value
							}
						}
						
						"        <Parameter Name='$($_.Name)'>" >> $filename
						"            <Description>"             >> $filename
						"                $description"          >> $filename
						"            </Description>"            >> $filename
						"            <DefaultValue>"            >> $filename
						"                $default"              >> $filename
						"            </DefaultValue>"           >> $filename
						"        </Parameter>"                  >> $filename
					}
				}
				
				"    </Parameters>"                           >> $filename
				"</Cmdlet>"                                   >> $filename
			}
		}
	}
}

[reflection.Assembly]::LoadFile((resolve-path '..\bin\debug\pscx.dll')) | New-HelpSkeleton